home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / HyperCard / RTF XCMDs 1.3 / FieldToRTF.c next >
C/C++ Source or Header  |  1993-03-29  |  6KB  |  270 lines

  1. /*
  2.  * This software is copyright 1992 by Robert Morris.
  3.  * You may freely redistribute this software as shareware
  4.  * if you do so in the same form as you got it. If you find
  5.  * this software useful, please send $12 to:
  6.  *   Robert Morris
  7.  *   P.O. Box 1044
  8.  *   Harvard Square Station
  9.  *   Cambridge, MA 02238
  10.  *   ecognome@aol.com
  11.  * If you incorporate any of this software in any kind of
  12.  * commercial product, please send $2 per copy distributed
  13.  * to the above address.
  14.  */
  15.  
  16. /*
  17.  * FieldToStyled(cardflag, fieldname)
  18.  * yields RTF for the field.
  19.  */
  20.  
  21. #include <HyperXCmd.h>
  22. #include <stdlib.h>
  23. #include <ctype.h>
  24. #include "SetUpA4.h"
  25. #include <string.h>
  26. #include "lists.h"
  27.  
  28. int sprintf(char *, const char *, ...);
  29. int sscanf(const char *, const char *, ...);
  30.  
  31. Handle HStr(char *);
  32. int **addfonttable();
  33. char *fontfamily();
  34.  
  35. pascal void
  36. main(paramPtr)
  37. XCmdPtr paramPtr;
  38. {
  39.     char **in;
  40.     int run, len, c;
  41.     long i, inlen;
  42.     TEHandle teh;
  43.     char fieldname[256], tmp[256], fname[256];
  44.     Boolean cardFld;
  45.     STElement *stp;
  46.     TEStyleHandle sh;
  47.     struct list out;
  48.     int **fonttable, nfonts;
  49.     char *dummy;
  50.     int fieldID, fieldNum;
  51.     
  52.     RememberA0();
  53.     SetUpA4();
  54.     
  55.     dummy = "Copyright 1991 by Differential Development.";
  56.     
  57.     if(paramPtr->paramCount != 2){
  58.         paramPtr->returnValue = HStr("error : usage FieldToRTF(cardflag, fieldname)\rCopyright 1992 Robert Morris.");
  59.         RestoreA4();
  60.         return;
  61.     }
  62.     
  63.     strncpy(fieldname, *(paramPtr->params[1]), sizeof(fieldname)-1);
  64.     fieldname[253] = '\0';    /* seems to be the max field name length */
  65.     
  66.     if(strcmp(*(paramPtr->params[0]), "false") == 0)
  67.         cardFld = FALSE;
  68.     else
  69.         cardFld = TRUE;
  70.         
  71.     if(isdigit(fieldname[0])){
  72.         fieldID = atoi(fieldname);
  73.         fieldNum = 0;
  74.     } else {
  75.         CtoPstr(fieldname);
  76.         fieldID = fieldNum = 0;
  77.     }
  78.     
  79.     teh = GetFieldTE(paramPtr, cardFld, fieldID, fieldNum, 
  80.                      fieldID ? 0 : (StringPtr)fieldname);
  81.     if(teh == 0 || paramPtr->result != xresSucc){
  82.         paramPtr->returnValue = HStr("error : no such field");
  83.         RestoreA4();
  84.         return;
  85.     }
  86.  
  87.     sh = GetStylHandle(teh);
  88.     if(sh == 0){
  89.         TEDispose(teh);
  90.         paramPtr->returnValue = HStr("error : bad GetStylHandle()");
  91.         RestoreA4();
  92.         return;
  93.     }
  94.     
  95.     in = (char **)TEGetText(teh);
  96.     if(in == 0){
  97.         TEDispose(teh);
  98.         paramPtr->returnValue = HStr("error : bad TEGetText()");
  99.         RestoreA4();
  100.         return;
  101.     }
  102.     
  103.     NewList((char **) 0, &out);
  104.     
  105.     AppendList(&out, "{\\rtf1\\mac");
  106.     
  107.     /*  figure out which fonts are used */
  108.     fonttable = 0;
  109.     for(run = 0; run < (*sh)->nRuns; run++){
  110.         stp = *((*sh)->styleTab) + ((*sh)->runs)[run].styleIndex;
  111.         if(findfonttable(fonttable, stp->stFont) < 0){
  112.             fonttable = addfonttable(fonttable, stp->stFont);
  113.             if(fonttable == 0){
  114. oom:
  115.                 TEDispose(teh);
  116.                 paramPtr->returnValue = HStr("error : out of memory");
  117.                 RestoreA4();
  118.                 return;
  119.             }
  120.         }
  121.     }
  122.     if(fonttable == 0){
  123.         fonttable = addfonttable(fonttable, 0);
  124.         if(fonttable == 0)
  125.             goto oom;
  126.     }
  127.         
  128.     /* spit out the list of fonts, using the Mac font numbers */
  129.     sprintf(tmp, "\\deff%u{\\fonttbl", (*fonttable)[0]);
  130.     AppendList(&out, tmp);
  131.     nfonts = GetHandleSize((Handle)fonttable) / sizeof(**fonttable);
  132.     for(i = 0; i < nfonts; i++){
  133.         GetFontName((*fonttable)[i], (StringPtr)fname);
  134.         PtoCstr(fname);
  135.         if(fname[0] == '\0')
  136.             sprintf(fname, "x%d", (*fonttable)[i]);
  137.         sprintf(tmp, "{\\f%u\\f%s %s;}",
  138.                 (*fonttable)[i], fontfamily((*fonttable)[i]), fname);
  139.         AppendList(&out, tmp);
  140.     }
  141.     AppendList(&out, "}\r");
  142.     DisposHandle((Handle)fonttable);
  143.     
  144.     run = -1;
  145.     inlen = (*teh)->teLength;
  146.     for(i = 0; i < inlen; i++){
  147.         if(run+1 < (*sh)->nRuns && i >= ((*sh)->runs)[run+1].startChar){
  148.             run++;
  149.             stp = *((*sh)->styleTab) + ((*sh)->runs)[run].styleIndex;    
  150.                 
  151.             /* reset to plain style, then set the font and size */
  152.             sprintf(tmp, "\\plain\\f%u\\fs%d",
  153.                     stp->stFont, stp->stSize * 2);
  154.             
  155.             if(stp->stFace & bold)
  156.                 strcat(tmp, "\\b");
  157.             if(stp->stFace & italic)
  158.                 strcat(tmp, "\\i");
  159.             if(stp->stFace & underline)
  160.                 strcat(tmp, "\\ulw");
  161.             if(stp->stFace & outline)
  162.                 strcat(tmp, "\\outl");
  163.             if(stp->stFace & shadow)
  164.                 strcat(tmp, "\\shad");
  165.             if(stp->stFace & 0x80)    /* HyperCard's grouped text */
  166.                 strcat(tmp, "\\hcgroup");
  167. #if 0
  168.             if(stp->stFace & condense)
  169.                 strcat(sname, "condense,");
  170.             if(stp->stFace & extend)
  171.                 strcat(sname, "extend,");
  172. #endif
  173.             strcat(tmp, " ");
  174.             AppendList(&out, tmp);
  175.         }
  176.         
  177.         c = (*in)[i];
  178.         if(c >= ' ' && c < 0177 && c != '\\' && c != '{' && c != '}'){
  179.             AppendListChar(&out, c);
  180.         } else if(c == '\r'){
  181.             AppendList(&out, "\\par\r");
  182.         } else {
  183.             sprintf(tmp, "\\'%02x", c & 0xff);
  184.             AppendList(&out, tmp);
  185.         }
  186.     }
  187.  
  188.     AppendList(&out, "}");
  189.     
  190.     TEDispose(teh);
  191.     
  192.     TrimList(&out);
  193.     if(out.h == 0){
  194.         paramPtr->returnValue = HStr("error : out of memory");
  195.         RestoreA4();
  196.         return;
  197.     }
  198.     
  199.     paramPtr->returnValue = out.h;
  200.         
  201.     RestoreA4();
  202. }
  203.  
  204. Handle
  205. HStr(str)
  206. char *str;
  207. {
  208.     Handle newHndl;
  209.     
  210.     newHndl = (Handle) NewHandle((long) strlen(str) + 1);
  211.     if(newHndl == 0)
  212.         return(0);
  213.     strcpy((char *) (*newHndl), str);
  214.     return(newHndl);
  215. }
  216.  
  217. int
  218. findfonttable(tb, f)
  219. int **tb;    /* a table of fonts for RTF */
  220. int f;        /* the Mac font # */
  221. {
  222.     int n, i;
  223.     
  224.     n = GetHandleSize((Handle)tb) / sizeof(int);
  225.     for(i = 0; i < n; i++)
  226.         if(f == (*tb)[i])
  227.             return(i);
  228.     return(-1);
  229. }
  230.  
  231. int **
  232. addfonttable(tb, f)
  233. int **tb;
  234. int f;    /* the Mac font # */
  235. {
  236.     int n;
  237.     
  238.     if(tb == 0){
  239.         tb = (int **)NewHandle(0L);
  240.         if(tb == 0)
  241.             return(0);
  242.     }
  243.     
  244.     n = GetHandleSize((Handle)tb) / sizeof(**tb);
  245.     SetHandleSize((Handle)tb, (n + 1) * sizeof(**tb));
  246.     if(MemError() != 0){
  247.         DisposHandle((Handle)tb);
  248.         return(0);
  249.     }
  250.     (*tb)[n] = f;
  251.     return(tb);
  252. }
  253.  
  254. char *
  255. fontfamily(f)
  256. int f;
  257. {
  258.     switch(f){
  259.     case 0: case geneva: case helvetica:
  260.         return("swiss");
  261.     case newYork: case times:
  262.         return("roman");
  263.     case monaco: case courier:
  264.         return("modern");
  265.     case symbol:
  266.         return("tech");
  267.     default:
  268.         return("nil");
  269.     }
  270. }